import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '@/views/Home.vue'
import Order from "@/views/Order.vue"
import MySelf from "@/views/MySelf.vue"
import ChooseFood from "@/views/ChooseFood.vue"
import Classify from "@/views/Classify.vue"
import Login from "@/views/Login.vue"
import FoodsDetails from "@/views/FoodsDetails.vue"
import ShopCar from "@/views/ShopCar.vue"
import SearchFoods from "@/views/SearchFoods.vue"
import store from "@/store"
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect:{
      name:"ChooseFood"
    }
  },
  {
    path: '/Home',
    component:Home,
    name: 'Home',
    children:[
      {
        path:"Order",
        component:Order,
        name:"Order",
        meta:{
          haveLogin:true,
          index:2
        }
      },
      {
        path:"MySelf",
        component:MySelf,
        name:"MySelf",
        meta:{
          index:4
        }
      },
      {
        path:"ChooseFood",
        component:ChooseFood,
        name:"ChooseFood",
        meta:{
          index:1
        }
      },
      {
        path:"Classify",
        component:Classify,
        name:"Classify",
        meta:{
          index:3
        }
      }
    ]
  },
  {
    path:"/Login",
    name:"Login",
    component:Login,
    meta:{
      index:11
    }
  },
  {
    path:"/FoodsDetails",
    name:"FoodsDetails",
    component:FoodsDetails,
    meta:{
      index:10
    }
  },
  {
    path:"/ShopCar",
    name:"ShopCar",
    component:ShopCar,
    meta:{
      haveLogin:true,
      index:12
    }
  },
  {
    path:"/SearchFoods",
    name:"SearchFoods",
    component:SearchFoods,
    meta:{
      index:13
    }
  },
  {
    path:"/OrderDetail",
    name:"OrderDetail",
    // 异步加载
    component:()=>import("@/views/OrderDetail.vue"),
    meta:{
      index:14
    }
  }
]

const router = new VueRouter({
  mode:"hash",
  routes
});

router.beforeEach((to,from,next)=>{
  if(to.meta.haveLogin){
    if(store.state.userInfo){
      next();
    } else {
      next({
        name:"Login"
      });
    }
  } else {
    next();
  }
});

export default router;
